<template>
  <div class="createPost-container">
    <el-form v-if="!infoLoading" ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">
              问题单号：<b style="padding-right:20px">931</b>
              房间：<b style="padding-right:20px">1101</b>
              报修人：<b style="padding-right:20px">张三</b>
              分类：<b style="padding-right:20px">设备故障</b>
            </div>

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">
              响应级别：<b style="padding-right:20px">紧急</b>
              发布时间：<b style="padding-right:20px">{{ postForm.publishTime }} 10:30:50</b>
              流程名称：<b style="padding-right:20px">领导审批</b>
            </div>

          </el-col>
        </el-row>

        <el-form-item v-if="!readOnly" label-width="60px" label="描述" prop="content" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
        </el-form-item>
        <el-form-item v-else prop="content" style="clear:both;">
          <h2>问题描述</h2>
          <div style="margin: 20px;font-size:16px;line-height:200%" v-html="postForm.content" />
        </el-form-item>

        <hr style="margin:30px 0;">
        <div class="postInfo-container">
          审批人：<b style="padding-right:20px">赵六</b>
          审批时间：<b style="padding-right:20px">{{ postForm.publishTime }} 09:30:50</b>
        </div>
        <el-form-item v-if="!readOnly" label-width="60px" label="分析" prop="content" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
        </el-form-item>
        <el-form-item v-else prop="content" style="clear:both;">
          <h2>审批信息</h2>
          <div style="margin: 20px;font-size:16px;line-height:200%" v-html="postForm.content" />
        </el-form-item>
        <hr style="margin:30px 0;">
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">
              接单人：<b style="padding-right:20px">李四</b>
              接单时间：<b style="padding-right:20px">{{ postForm.publishTime }} 09:30:50</b>
            </div>

          </el-col>
        </el-row>
        <el-form-item v-if="!readOnly" label-width="60px" label="分析" prop="content" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
        </el-form-item>
        <el-form-item v-else prop="content" style="clear:both;">
          <h2>问题分析</h2>
          <div style="margin: 20px;font-size:16px;line-height:200%" v-html="postForm.content" />
        </el-form-item>

        <el-form-item v-if="!readOnly" label-width="60px" label="解决" prop="content" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
        </el-form-item>
        <el-form-item v-else prop="content" style="clear:both;">
          <h2>解决方案</h2>
          <div style="margin: 20px;font-size:16px;line-height:200%" v-html="postForm.content" />
        </el-form-item>
        <hr style="margin:30px 0;">
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">
              结束人：<b style="padding-right:20px">王五</b>
              结束时间：<b style="padding-right:20px">{{ postForm.publishTime }} 10:30:50</b>
            </div>
            <hr style="margin:30px 0;">
            <div class="postInfo-container">
              评价人：<b style="padding-right:20px">张三</b>
              评价：<b style="padding-right:20px">满意</b>
              评价时间：<b style="padding-right:20px">{{ postForm.publishTime }} 11:30:50</b>
            </div>
            <div class="postInfo-container">
              评价人：<b style="padding-right:20px">王五</b>
              评价：<b style="padding-right:20px">一般</b>
              评价时间：<b style="padding-right:20px">{{ postForm.publishTime }} 12:30:50</b>
            </div>

          </el-col>
        </el-row>

        <hr style="margin:30px 0;">
        <h2>流程处理历史记录</h2>
        <el-table
          ref="replyTable"
          v-loading="replyListLoading"
          :data="replyList"
          border
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column align="center" prop="dataCreateDatetime" label="处理时间" width="150" />
          <el-table-column align="center" prop="user" label="处理人" width="100" />

          <el-table-column prop="content" align="center" label="处理" />

        </el-table>
        <el-form-item v-if="!readOnly" style="margin-bottom: 30px;">
          <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
        </el-form-item>

      </div>
    </el-form>
  </div>
</template>

<script>

const defaultForm = {
  classCode: '',
  content: '', // 文章内容
  files: [], // 文章图片
  id: null,
  isTop: 0,
  publishTime: '', // 前台展示时间
  title: '' // 文章题目
}

export default {
  name: 'ArticleDetail',
  props: {
    readOnly: {
      type: Boolean,
      default: false
    },
    infoid: {
      type: Number,
      default: 0
    },
    timestamp: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: '请将表单填写完整',
          type: 'error'
        })
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      myHeaders: {},
      imageUrl: '',
      infoLoading: false,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      classOption: [],
      rules: {
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }]
      },
      tempRoute: {}
    }
  },
  watch: {
    timestamp: {
      handler(val, oldVal) {
        console.log('timestamp:' + val)
        this.getData(this.infoid)
      },
      immediate: true
    }
  },
  mounted() {
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
    const day = new Date()
    day.setTime(day.getTime())
    this.postForm.publishTime = day.getFullYear() + '-' + this.add0(day.getMonth() + 1) + '-' + this.add0(day.getDate())
  },
  created() {
    this.getDic()

    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    add0(m) {
      return m < 10 ? '0' + m : m
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      console.log('test')
      if (res.success) {
        this.$message({
          message: '上传成功！',
          type: 'success'
        })
        this.imageUrl = res.result[0].id
        // this.fileList = []
        // this.files = []
      } else {
        this.$message({
          message: '上传失败:' + res.message,
          type: 'error'
        })
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isGIF = file.type === 'image/gif'
      const isLt20M = file.size / 1024 / 1024 < 20

      if (!isJPG && !isPNG && !isGIF) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
        return false
      }
      if (!isLt20M) {
        this.$message.error('上传图片大小不能超过 20MB!')
        return false
      }
    },
    getData(id) {
      if (id) {
        this.infoLoading = true
        this.$http
          .get('/api/busProblemFeedback/getInfoById?&id=' + id)
          .then(response => {
            this.infoLoading = false
            if (response.data.success === true) {
              this.postForm = response.data.result
            } else {
              console.log('获取失败:' + response.data.message)
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
            this.infoLoading = false
          })
      }
    },
    getDic() {
      const code = '01'
      this.$http
        .get('/api/sysDict/getByCode?enabled=true&code=' + code)
        .then(response => {
          this.stopDicLoading = false
          if (response.data.success === true) {
            this.classOption = response.data.result
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.stopDicLoading = false
        })
    },
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          const para = {
            classCode: this.postForm.classCode,
            content: this.postForm.content,
            files: [],
            id: this.postForm.id ? this.postForm.id : null,
            isTop: this.postForm.isTop ? 1 : 0,
            publishTime: this.postForm.publishTime,
            title: this.postForm.title,
            state: 0,
            coverImg: this.imageUrl
          }
          this.loading = true
          this.$http
            .post('/api/busProblemFeedback/save', para)
            .then(response => {
              this.loading = false
              if (response.data.success === true) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$emit('dialogHide')
              } else {
                console.log('提交失败:' + response.data.message)
                this.$message({
                  message: '提交失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(function(error) {
              console.log(error)
              this.loading = false
            })
          this.postForm.status = 'published'
          this.postForm = Object.assign({}, defaultForm)
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    // padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
